package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.CargoDao;
import com.nc.delivery_project.server.dao.OrderDao;
import com.nc.delivery_project.server.dao.OrderWaypointDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.domain.Order;
import com.nc.delivery_project.server.domain.OrderWaypoint;
import com.nc.delivery_project.server.domain.Payment;
import com.nc.delivery_project.server.domain.User;
import com.nc.delivery_project.server.util.CRUD;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/12/15
 * Time: 4:37 PM
 */
public class OrderService  extends BaseService<Order> {

    public OrderService() {
        this(new OrderDao());
    }

    public OrderService(Connection connection, boolean isAutoCloseConnection) {
        super(new OrderDao(), connection, isAutoCloseConnection);
    }

    public OrderService(AbstractDao<Order> dao) {
        super(dao);
    }


    @Override
    public Order read(int id)
    {
        Order order = null;
        ArrayList<OrderWaypoint> orderWaypoints=null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            order = dao.read(id);

            if (order != null)
            {
                User sender = new UserService(conn, false).read(order.getSender().getId());
                User receiver = new UserService(conn, false).read(order.getReceiver().getId());
                Payment payment = new PaymentService(conn, false).read(order.getPayment().getId());
                List<OrderWaypoint> ow = new OrderWaypointService(conn, false).readByOrderId(order.getId());
                if (sender != null)
                {
                    order.setSender(sender);
                }
                if (receiver!=null)
                {
                    order.setReceiver(receiver);
                }
                if (payment!=null)
                {
                    order.setPayment(payment);
                }
                if (orderWaypoints!=null)
                {
                    order.setOrderWaypoints(orderWaypoints);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return order;
    }

    @Override
    public List<Order> read()
    {
        List<Order> orders = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orders = dao.read();
                for (Order order : orders)
                {
                    order.setSender(new UserService(conn, false).read(order.getSender().getId()));
                    order.setReceiver(new UserService(conn, false).read(order.getReceiver().getId()));
                    order.setPayment(new PaymentService(conn, false).read(order.getPayment().getId()));
                    order.setOrderWaypoints(new OrderWaypointService(conn, false).readByOrderId(order.getId()));
                }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orders;
    }

    public List<Order> readBySenderId(String sid)
    {
        int id=Integer.parseInt(sid);
        List<Order> orders = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orders = ((OrderDao)dao).readBySenderId(id);

            for (Order order : orders)
            {
                order.setSender(new UserService(conn, false).read(id));
                order.setReceiver(new UserService(conn, false).read(order.getReceiver().getId()));
                order.setPayment(new PaymentService(conn, false).read(order.getPayment().getId()));
                order.setOrderWaypoints(new OrderWaypointService(conn, false).readByOrderId(order.getId()));
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return orders;
    }

    public List<Order> readByReceiverId(String sid)
    {
        List<Order> orders = null;
        int id=Integer.parseInt(sid);
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            orders = ((OrderDao)dao).readByReceiverId(id);

            for (Order order : orders)
            {
                order.setSender(new UserService(conn, false).read(order.getSender().getId()));
                order.setReceiver(new UserService(conn, false).read(id));
                order.setPayment(new PaymentService(conn, false).read(order.getPayment().getId()));
                order.setOrderWaypoints(new OrderWaypointService(conn, false).readByOrderId(order.getId()));
            }
        }

        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return orders;
    }
}

